import { storage } from '@/utils/Storage';
import fontkit from '@pdf-lib/fontkit';
import { message } from 'antd';
import { PDFDocument, rgb } from 'pdf-lib';
export const addDefaultMark = async (url, dataSource = []) => {
  try {
    const token = storage.get('token');
    const baseUrl = `/api/getPdf/${url}`;
    const existingPdfBytes = await fetch(baseUrl, {
      headers: {
        Authorization: `Bearer ${token}`,
      },
    }).then((res) => res.arrayBuffer());
    const pdfDoc = await PDFDocument.load(existingPdfBytes);
    const fontBytes = await fetch('/fonts/simhei.ttf').then((res) =>
      res.arrayBuffer(),
    );
    pdfDoc.registerFontkit(fontkit);
    const fontkitFile = await pdfDoc.embedFont(fontBytes);
    const pages = pdfDoc.getPages();
    const pageDataSource = new Array(pages.length).fill([]);
    dataSource.forEach((item) => {
      pageDataSource[item.thesis_page_number - 1] = [
        ...pageDataSource[item.thesis_page_number - 1],
        {
          description: item.description,
          content: JSON.parse(item.draw_content),
          type: item.draw_type,
        },
      ];
    });
    for (let i = 0; i < pages.length; i++) {
      const page = pages[i];
      for (let j = 0; j < pageDataSource[i].length; j++) {
        const pageEle = pageDataSource[i][j];
        if (pageEle) {
          if (pageEle.type === 'Text') {
            page.drawText(pageEle.description, {
              opacity: 0.8,
              color: rgb(0.95, 0.1, 0.1),
              size: 16,
              font: fontkitFile,
              ...pageEle.content,
            });
          } else if (pageEle.type === 'Line') {
            page.drawLine({
              ...pageEle.content,
              color: rgb(0.95, 0.1, 0.1),
            });
          } else if (pageEle.type === 'Rectangle') {
            page.drawRectangle({
              ...pageEle.content,
              borderColor: rgb(0.95, 0.1, 0.1),
            });
          }
        }
      }
    }
    // 生成 unit64Arrary文件流
    const pdfBytes = await pdfDoc.save();
    let blobData = new Blob([pdfBytes], { type: 'application/pdf;Base64' });

    // 返回一个新的 url 链接
    return window.URL.createObjectURL(blobData);
  } catch (err) {
    message.error('pdf渲染失败，请刷新页面');
  }
};
export const baseColumns = [
  {
    title: '论文名称',
    dataIndex: 'thesis_name',
    hideInTable: true,
  },
  {
    title: '学生姓名',
    dataIndex: 'student_name',
    hideInTable: true,
  },
  {
    title: '学生学号',
    dataIndex: 'student_jsnumber',
    hideInTable: true,
  },
  {
    title: '学生班级',
    dataIndex: 'class_id',
    hideInTable: true,
    valueType: 'select',
    valueEnum: new Map([]),
  },
  {
    title: '论文状态',
    dataIndex: 'thesis_status',
    hideInTable: true,
    valueType: 'select',
    valueEnum: new Map([
      ['待审查', '待审查'],
      ['审查中', '审查中'],
      ['已完成', '已完成'],
    ]),
  },
  {
    title: '序号',
    dataIndex: 'id',
    hideInSearch: true,
    width: 60,
    render: (text, record, index) => {
      return index + 1;
    },
  },
  {
    title: '论文名称',
    dataIndex: 'thesis_name',
    hideInSearch: true,
  },
  {
    title: '学生姓名',
    dataIndex: 'student_thesis',
    hideInSearch: true,
    render: (value, record, index) => {
      return value.name;
    },
  },
  {
    title: '学生学号',
    dataIndex: 'student_thesis',
    hideInSearch: true,
    render: (value, record, index) => {
      return value.js_number;
    },
  },
  {
    title: '学生班级',
    dataIndex: 'student_thesis',
    hideInSearch: true,
    render: (value, record, index) => {
      return value.class[0]?.class_name;
    },
  },
  {
    title: '论文状态',
    dataIndex: 'thesis_status',
    hideInSearch: true,
  },
];
